﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class CompetitionsController : Controller
    {
        private atletisEntities db = new atletisEntities();

        private const string COMPETITIONS_YEAR = "CompetitionsYear";
        private const string COMPETITIONS_MONTH = "CompetitionsMonth";

        // GET: /Competitions/
        public ViewResult Index(int? Year, int? Month)
        {
            if (Year == null)
            {
                if (Session[COMPETITIONS_YEAR] != null)
                {
                    Year = Convert.ToInt32(Session[COMPETITIONS_YEAR]);
                }
                else
                {
                    Year = DateTime.Now.Year;
                }
            }

            if (Month == null)
            {
                if (Session[COMPETITIONS_MONTH] != null)
                {
                    Month = Convert.ToInt32(Session[COMPETITIONS_MONTH]);
                }
                else
                {
                    Month = DateTime.Now.Month;
                }
            }

            Session[COMPETITIONS_MONTH] = Month;
            Session[COMPETITIONS_YEAR] = Year;

            ViewBag.Year = new SelectList(db.Years, "Year", "Year", Year);
            ViewBag.Month = new SelectList(Months.GetMonths(), "ID", "Name", Month);

            var model = db.Competitions
                .Where(c => c.DateFrom.Value.Year == Year & c.DateFrom.Value.Month == Month)
                .OrderBy(c => c.DateFrom)
                .ToList();

            return View(model);
        }

        // GET: /Competitions/Details/5
        public ActionResult Details(int id)
        {
            Competitions competitions = db.Competitions.Single(c => c.ID == id);
            if (competitions == null)
            {
                return HttpNotFound();
            }
            return View(competitions);
        }

        // GET: /Competitions/Create
        public ActionResult Create()
        {
            return View();
        } 

        // POST: /Competitions/Create
        [HttpPost]
        public ActionResult Create(Competitions competitions)
        {
            competitions.MeasurementId = 1;
            competitions.WebID = 0;
            
            if (ModelState.IsValid)
            {
                db.Competitions.AddObject(competitions);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(competitions);
        }
        
        // GET: /Competitions/Edit/5
        public ActionResult Edit(int id)
        {
            Competitions competitions = db.Competitions.Single(c => c.ID == id);
            return View(competitions);
        }

        // POST: /Competitions/Edit/5
        [HttpPost]
        public ActionResult Edit(Competitions competitions)
        {
            if (ModelState.IsValid)
            {
                db.Competitions.Attach(competitions);
                db.ObjectStateManager.ChangeObjectState(competitions, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(competitions);
        }

        // GET: /Competitions/Delete/5
        public ActionResult Delete(int id)
        {
            Competitions competitions = db.Competitions.Single(c => c.ID == id);
            return View(competitions);
        }

        // POST: /Competitions/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Competitions competitions = db.Competitions.Single(c => c.ID == id);
            db.Competitions.DeleteObject(competitions);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}